// pages/homework/homework.js
const App = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileUrl: '', // 作业地址
    submitButton: false, //作业是否可提交
    homeWorkData: {},
    professionalScore: {
      name: '专业',
      value: 0,
      type: 'professionalScore'
    }, //专业
    attitudeScore: {
      name: '态度',
      value: 0,
      type: 'attitudeScore'
    }, //态度
    timetableDetailedId: '',
    studentUserId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.timetableId && options.type) {
      this.getHomework(options.timetableId, options.studentUserId)
    }
  },
  async getHomework(timetableDetailedId, studentUserId) {
    // 作业查看接口
    let data = await App.$axios.request({
      url: "/parent/myCurriculum/homework",
      data: {
        timetableDetailedId,
        studentUserId
      },
      method: 'GET',
      loading: true,
    })
    if (data) {
      this.setData({
        homeWorkData: data,
        'professionalScore.value': data.professionalScore,
        'attitudeScore.value': data.attitudeScore,
        fileUrl: data.videoUrl
      })
    }
    this.setData({
      timetableDetailedId: timetableDetailedId,
      studentUserId: studentUserId
    })
  },
  async postHomework() {
    // 上传作业
    try {
      let files = await wx.chooseMedia({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        mediaType: ['video'], // 文件类型 image  video  mix
        sourceType: ['album']
      });
      let file = files.tempFiles[0]
      let suffix = file.tempFilePath.substring(file.tempFilePath.indexOf('.'), file.tempFilePath.length)
      let url = await App.$axios.request({
        url: `/file/upload`,
        method: 'FILE',
        loading: true,
        filePath: file.tempFilePath,
        name: 'file', // 文件字段名，默认为file
        formData: {
          name: `feedbackFile${suffix}`
        }
      })
      console.log(url)
      if (!url) return
      this.setData({
        fileUrl: url,
        submitButton: true
      })
    } catch (error) {
      console.log(error)
    }
  },
  async submit() {
    // 作业提交
    let data = await App.$axios.request({
      url: "/parent/myCurriculum/uploadHomework",
      data: {
        timeTableDeletedId: this.data.timetableDetailedId,
        studentUserId: this.data.studentUserId,
        videoUrl: this.data.fileUrl
      },
      method: 'PUT',
      loading: true,
    })
    if (data) {
      this.setData({
        submitButton: false
      })
      wx.showToast({
        title: '提交成功',
        icon: 'none'
      })
    }
  }
})